import java.util.ArrayList;
import java.util.List;

class Solution2 {
    List<String> ret;
    public List<String> letterCasePermutation(String s) {
        ret = new ArrayList<>();
        ret.add(s);
        StringBuffer stringbuffer = new StringBuffer(s);
        dfs(stringbuffer, 0);
        return ret;
    }
    private void dfs(StringBuffer stringbuffer, int index) {
        for(int i = index; i < stringbuffer.length(); i++) {
            if(Character.isLowerCase(stringbuffer.charAt(i))) {
                stringbuffer.replace(i, i+1, Character.toString(stringbuffer.charAt(i) - 32));
                ret.add(stringbuffer.toString());
                dfs(stringbuffer, i + 1);
                stringbuffer.replace(i, i+1, Character.toString(stringbuffer.charAt(i) + 32));
            }else if(Character.isUpperCase(stringbuffer.charAt(i))) {
                stringbuffer.replace(i, i+1, Character.toString(stringbuffer.charAt(i) + 32));
                ret.add(stringbuffer.toString());
                dfs(stringbuffer, i + 1);
                stringbuffer.replace(i, i+1, Character.toString(stringbuffer.charAt(i) - 32));
            }
        }
    }
}